<template>
    <div class="start-logo">
        <el-container>
            <el-header class="top-nav-header">
                <el-container class="nav-inner-container">
                    <el-aside width="220px" class="nav-logo-section">
                        <img src="D:\java116\code\qian\project\vehicle\src\assets\carlogo.png" class="nav-logo"
                            alt="汽车租赁系统Logo" />
                        <h1 class="nav-system-title">汽车租赁系统</h1>
                    </el-aside>

                    <!-- 中间导航菜单区 -->
                    <el-main class="nav-menu-section">
                        <el-menu class="custom-horizontal-menu" :unique-opened="true" mode="horizontal" router
                            active-text-color="#1e40af" background-color="transparent">
                            <el-menu-item index="/MainFrameView">
                                <h1>首页</h1>
                            </el-menu-item>
                            <template v-for="item in mainObj.menus" :key="item.mid">false
                                <!-- 若有子菜单 -->
                                <el-sub-menu v-if="item.children && item.children.length > 0"
                                    :index="item.mid.toString()" class="menu-parent-item">
                                    <template #title>
                                        <el-icon class="menu-icon">
                                            <component :is="item.icon"></component>
                                        </el-icon>
                                        <span class="menu-text">{{ item.mname }}</span>
                                    </template>
                                    <el-menu-item v-for="child in item.children" :key="child.mid"
                                        :index="child.mid.toString()" :route="child.route" class="menu-child-item">
                                        <el-icon class="menu-child-icon">
                                            <component :is="child.icon"></component>
                                        </el-icon>
                                        <span class="menu-child-text">{{ child.mname }}</span>
                                    </el-menu-item>
                                </el-sub-menu>

                                <!-- 若无子菜单 -->
                                <el-menu-item v-else :index="item.route" class="menu-single-item">
                                    <el-icon class="menu-icon">
                                        <component :is="item.icon"></component>
                                    </el-icon>
                                    <span class="menu-text">{{ item.mname }}</span>
                                </el-menu-item>
                            </template>
                        </el-menu>
                    </el-main>

                    <!-- 右侧用户信息区 -->
                    <el-aside width="220px" class="nav-user-section">
                        <div class="user-info-container">
                            <el-avatar :size="mainObj.avatar.size || 42" :src="store.userinfo.imgPath"
                                class="user-avatar" id="userImage" />
                            <div class="user-operate-box">
                                <span class="user-name">{{ store.userinfo.username }}</span>
                                <el-dropdown class="user-dropdown" trigger="hover">
                                    <el-icon class="dropdown-trigger-icon">
                                        <Setting />
                                    </el-icon>
                                    <template #dropdown>
                                        <el-dropdown-menu class="custom-dropdown" align="end">
                                            <el-dropdown-item @click="updateUser" class="dropdown-item">
                                                <el-icon class="dropdown-item-icon">
                                                    <User />
                                                </el-icon>
                                                修改个人信息
                                            </el-dropdown-item>
                                            <el-dropdown-item class="dropdown-item">
                                                <el-icon class="dropdown-item-icon">
                                                    <Camera />
                                                </el-icon>
                                                修改头像
                                            </el-dropdown-item>
                                            <el-dropdown-item class="dropdown-item" @click="exit()"
                                                style="color: #dc2626;">
                                                <el-icon class="dropdown-item-icon">
                                                    <Logout />
                                                </el-icon>
                                                退出
                                            </el-dropdown-item>
                                        </el-dropdown-menu>
                                    </template>
                                </el-dropdown>
                            </div>
                        </div>
                    </el-aside>
                </el-container>
            </el-header>

            <el-main>
                <router-view />
            </el-main>
        </el-container>
    </div>
</template>

<script setup>
import request from '../utils/request';
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { Setting, User, Camera } from '@element-plus/icons-vue';
import { useUserStore } from '@/stores/userStore';
import ElementPlus from 'element-plus'

let store = useUserStore();
let router = useRouter();


var mainObj = reactive({
    menus: [],
    avatar: {
        size: 42, // 适配导航栏高度的头像尺寸
    }
});

onMounted(async () => {
    // console.log(store.userinfo.username);

    let res = await request.get(`menu/queryMenuByName/${store.userinfo.username}`)
    if (res.data.success) {
        mainObj.menus = res.data.data;
    }
})
const updateUser = () => {
    router.push("/MainFrameView/otherMessage");
}
const exit = () => {
    store.loginOut();
    router.push("/");
}

</script>

<style scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}

img {
    border-radius: 8px;
    transition: transform 0.3s;
}

img:hover {
    transform: scale(1.02);
}

.el-card__header {
    font-weight: bold;
    text-align: center;
}

.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: center;
}

.button-row>* {
    margin: 0;
}

.top-nav-header {
    background: linear-gradient(120deg, #be58f5 0%, #5270d3 100%);
    height: 76px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
    padding: 0 24px;
    display: flex;
    align-items: center;
}

/* 导航栏内部布局容器 */
.nav-inner-container {
    width: 100%;
    height: 100%;
    align-items: center;
}

/* 1. 左侧Logo与标题区 */
.nav-logo-section {
    display: flex;
    align-items: center;
    gap: 12px;
}

.nav-logo {
    width: 48px;
    height: 48px;
    border-radius: 8px;
    /* 防止Logo拉伸变形 */
    object-fit: cover;
    /* 轻微边框突出Logo */
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.nav-system-title {
    font-size: 22px;
    color: #ffffff;
    font-weight: 600;
    letter-spacing: 1px;
    /* 字间距优化，提升标题质感 */
    margin: 0;
}

/* 2. 中间导航菜单区 */
.nav-menu-section {
    padding: 0 !important;
    /* 清除Element默认内边距 */
}

/* 水平菜单主体 */
.custom-horizontal-menu {
    height: 100%;
    /* 菜单项高度与导航栏一致 */
    --el-menu-item-height: 76px;
    /* 清除默认下边框 */
    border-bottom: none;
}

/* 一级菜单基础样式 */
.menu-parent-item,
.menu-single-item {
    color: rgba(255, 255, 255, 0.9) !important;
    /* 默认文字色 */
    font-size: 15px;
    position: relative;
    /* 平滑过渡效果 */
    transition: all 0.3s ease;
}

/* 一级菜单hover效果 */
.menu-parent-item:hover,
.menu-single-item:hover {
    color: #ffffff !important;
    background-color: rgba(255, 255, 255, 0.1) !important;
}

/* 一级菜单选中状态 */
.menu-parent-item.is-active,
.menu-single-item.is-active {
    color: #ffffff !important;
}

/* 选中菜单底部标识线 */
.menu-single-item.is-active::after,
.menu-parent-item.is-active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background-color: #ffffff;
    border-radius: 3px 3px 0 0;
}

/* 菜单图标样式 */
.menu-icon {
    margin-right: 8px;
    font-size: 16px;
}

/* 菜单文字样式 */
.menu-text {
    letter-spacing: 0.5px;
}

/* 子菜单样式 */
.menu-child-item {
    color: #333333 !important;
    font-size: 14px;
    transition: background-color 0.2s ease;
}

.menu-child-item:hover {
    background-color: rgba(59, 130, 246, 0.1) !important;
    color: #1e40af !important;
}

.menu-child-icon {
    margin-right: 6px;
    font-size: 14px;
}

/* 登录按钮 */
.nav-user-section {
    display: flex;
    /* 末端对齐 */
    justify-content: flex-end;
    /* 内容右对齐 */
    align-items: center;
    padding-right: 8px;
}

/* 用户信息容器 */
.user-info-container {
    display: flex;
    align-items: center;
    /* gap容器内部子元素间距 */
    gap: 12px;
    padding: 6px 12px;
    border-radius: 30px;
    /* 颜色变化时间：ease先慢后快再慢 */
    transition: background-color 0.3s ease;
}

/* 用户登录按钮hover效果 */
.user-info-container:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* 用户头像 */
.user-avatar {
    border: 2px solid rgba(255, 255, 255, 0.3);
    cursor: pointer;
    transition: transform 0.3s ease;
}

.user-avatar:hover {
    /* 头像hover放大 */
    transform: scale(1.3);
}

/* 用户操作盒子 */
.user-operate-box {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* 用户名 */
.user-name {
    color: #ffffff;
    font-size: 14px;
    font-weight: 500;
    max-width: 80px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    /* 用户名过长时省略，避免布局错乱 */
}

/* 下拉触发图标 */
.dropdown-trigger-icon {
    color: rgba(255, 255, 255, 0.9);
    font-size: 18px;
    cursor: pointer;
    transition: color 0.3s ease;
}

.dropdown-trigger-icon:hover {
    color: #ffffff;
}

/* 自定义下拉菜单 */
.custom-dropdown {
    width: 160px;
    border-radius: 8px;
    /* 下拉菜单阴影 */
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    padding: 6px 0;
    /* 清除默认边框 */
    border: none;
}

/* 下拉菜单项 */
.dropdown-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    font-size: 14px;
}

.dropdown-item:hover {
    background-color: rgba(59, 130, 246, 0.1);
}

/* 下拉菜单项图标 */
.dropdown-item-icon {
    font-size: 14px;
    color: #3b82f6;
}

/* 背景图 */
.start-logo {
    background-image: url("../assets/bgc.jpg");
    background-size: cover;
}
</style>